#include <iostream>
using namespace std;

#include "healthClub.h"
#pragma warning(disable : 4996)


HealthClub::HealthClub(const char* name) : numOfDevices(0), numOfTrainers(0), phisicalSizeOfDevices(2)
{
	setName(name);
	allDevices = new Device*[phisicalSizeOfDevices];
}

HealthClub::~HealthClub()
{
	for (int i=0 ; i < numOfDevices ; i++)
		delete allDevices[i];
	delete []allDevices;

	for (int i=0 ; i < numOfTrainers ; i++)
		delete allTrainers[i];

}

void HealthClub::setName(const char* name)
{
	strcpy(this->name, name);
}

HealthClub::RegistrationStatus HealthClub::registerTrainer(const char* name)
{
	if (numOfTrainers == MAX_TRAINERS)
		return REGISTER_FAILED_CLUB_IS_FOOL;

	allTrainers[numOfTrainers] = new Trainer(name);
	numOfTrainers++;
	return REGISTER_SUCCEED;
}

void HealthClub::addDevice(const Device &D)
{
	if (numOfDevices == phisicalSizeOfDevices)
	{
		phisicalSizeOfDevices *= 2;
		Device** temp = new Device*[phisicalSizeOfDevices];
		for (int i=0 ; i < numOfDevices ; i++)
			temp[i] = allDevices[i];
		delete []allDevices;
		allDevices = temp;
	}

	allDevices[numOfDevices] = D.Clone();
	numOfDevices++;
}


Trainer* const HealthClub::getTrainer(const char* name) const
{
	for (int i=0 ; i < numOfTrainers ; i++)
		if (strcmp(name, allTrainers[i]->getName()) == 0)
			return allTrainers[i];
	return NULL;
}

const Device* HealthClub::getDevice (const char* name) const
{
	for (int i=0 ; i < numOfDevices ; i++)
		if (strcmp(name, allDevices[i]->getName()) == 0)
			return allDevices[i];
	return NULL;
}

const HealthClub & HealthClub::operator=(const HealthClub & HC)				//#WD[2] RonnyR 3.12
{
	this->~HealthClub();
	phisicalSizeOfDevices = 2;
	allDevices = new Device*[phisicalSizeOfDevices];
	numOfDevices = 0;
	numOfTrainers = 0;
	strcpy(name,HC.getName());
	for(int i=0;i<HC.getNumDevices();i++)
		addDevice(HC.getAllDevices()[i]->getName());

	for(int i=0;i<HC.getNumTrainers();i++)
		registerTrainer((*HC.allTrainers[i]).getName());	

	return *this;



}
ostream & operator<<(ostream & os,const HealthClub & HC)    //#WD[2] RonnyR 3.12
{
os<<"["<<HC.getName()<<"]"<<endl;
for(int i=0;i<HC.numOfTrainers;i++)
	{
		os<<*HC.allTrainers[i];
		for(int j=0;j<HC.allTrainers[i]->getProgram().getNumOfExercises();j++)
			os<<*HC.allTrainers[i]->getProgram().getAllExercises()[j];
	}

return os;
}